package test1;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class StateDemo
{
	/*********************************** BEGIN CONSTANTS *******************************************/
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final int ZNEAR = 1;

	/*********************************** END CONSTANTS *******************************************/

	private static enum State
	{
		INTRO, MAIN_MENU, GAME;
	}

	private State state = State.INTRO;

	public StateDemo()
	{
		try
		{
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.setTitle("State Demo");
			Display.create();
		} catch (LWJGLException e)
		{
			e.printStackTrace();
		}

		// Initialize OpenGL
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, WIDTH, HEIGHT, 0, ZNEAR, -1);
		glMatrixMode(GL_MODELVIEW);

		while (!Display.isCloseRequested())
		{
			CheckInput();
			glClear(GL_COLOR_BUFFER_BIT);

			render();

			Display.update();
			Display.sync(60);
		}
		Display.destroy();
	}

	public static void main(String[] args)
	{
		new StateDemo();

	}

	private void render()
	{
		switch (state)
		{
		case INTRO:
			glColor3f(1.0f, 0f, 0f);
			glRectf(0, 0, WIDTH, HEIGHT);
			break;
		case GAME:
			glColor3f(0f, 1.0f, 0f);
			glRectf(0, 0, WIDTH, HEIGHT);
			break;
		case MAIN_MENU:
			glColor3f(0f, 0f, 1.0f);
			glRectf(0, 0, WIDTH, HEIGHT);
			break;
		}
	}

	private void CheckInput()
	{
		switch (state)
		{
		case INTRO:
			if (Keyboard.isKeyDown(Keyboard.KEY_S))
			{
				state = State.MAIN_MENU;
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
			{
				Display.destroy();
				System.exit(0);
			}
			break;
		case GAME:
			if (Keyboard.isKeyDown(Keyboard.KEY_BACK))
			{
				state = State.MAIN_MENU;
			}
			break;
		case MAIN_MENU:
			if (Keyboard.isKeyDown(Keyboard.KEY_RETURN))
			{
				state = State.GAME;
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
			{
				state = State.INTRO;
			}
			break;

		}
	}
}
